{% extends 'base.html' %}
{% block content %}
	<h3 class="common_title">确认收货地址</h3>

	<div class="common_list_con clearfix">
		<dl>
			<dt>寄送到：</dt>
			<dd><input type="radio">{{ user.uaddress }} （{{ user.ushou }} 收） {{ user.uphone }}</dd>
		</dl>
		<a href="/user/site/" class="edit_site">编辑收货地址</a>

	</div>
	
	<h3 class="common_title">支付方式</h3>	
	<div class="common_list_con clearfix">
		<div class="pay_style_con clearfix">
			<input type="radio" name="pay_style" checked>
			<label class="cash">货到付款</label>
			<input type="radio" name="pay_style">
			<label class="weixin">微信支付</label>
			<input type="radio" name="pay_style">
			<label class="zhifubao"></label>
			<input type="radio" name="pay_style">
			<label class="bank">银行卡支付</label>
		</div>
	</div>

	<h3 class="common_title">商品列表</h3>
	
	<div class="common_list_con clearfix">
		<ul class="goods_list_th clearfix">
			<li class="col01">商品名称</li>
			<li class="col02">商品单位</li>
			<li class="col03">商品价格</li>
			<li class="col04">数量</li>
			<li class="col05">小计</li>		
		</ul>
        {% for cart in carts %}
		<ul class="goods_list_td clearfix" id="{{ cart.id }}">
			<li class="col01">{{ forloop.counter }}</li>
			<li class="col02"><img src="/static/{{ cart.goods.gpic }}"></li>
			<li class="col03">{{ cart.goods.gtitle }}</li>
			<li class="col04">{{ cart.goods.gunit }}</li>
			<li class="col05">{{ cart.goods.gprice }}</li>
			<li class="col06">{{ cart.count }}</li>
			<li class="col07">25.80元</li>
		</ul>
        {% endfor %}
	</div>

	<h3 class="common_title">总金额结算</h3>

	<div class="common_list_con clearfix">
		<div class="settle_con">
			<div class="total_goods_count">共<em>2</em>件商品，总金额<b>42.60元</b></div>
			<div class="transit">运费：<b>10元</b></div>
			<div class="total_pay">实付款：<b>52.60元</b></div>
		</div>
	</div>

	<div class="order_submit clearfix">
        <a href="javascript:;" id="order_btn">提交订单</a>
	</div>	
{% endblock content %}

{% block bottom %}
	<div class="popup_con">
		<div class="popup">
			<p>订单提交成功！</p>
		</div>
		
		<div class="mask"></div>
	</div>
<script type="text/javascript">
    $(function(){
        //总价
        var total1 = 0;
        //总个数
        var total_count = 0;
         //计算小计和总计
        $('.col07').each(function(){
            //获取数量
            var count = $(this).prev().text();
            //获取单价
            var price = $(this).prev().prev().text();
            //计算小计
            var total0 = parseFloat(count)*parseFloat(price);
            $(this).text(total0.toFixed(2));
            total1 += total0;
            total_count++;
        });
        //显示总计
        $('.total_goods_count b').html(total1.toFixed(2));
        //总个数
        $('.total_goods_count em').html(total_count);
        //加上运费
        $('.total_pay b').text((total1+10).toFixed(2));
        /* 下订单触发click事件，用来校验订单是否下成功，根据AJAX返回值展示不同页面*/
        $('#order_btn').click(function(){
            //商品id列表
            var cart_ids = [];
            //获取收货地址
            var address = $('.common_list_con dl dd').text();
            //获取总金额
            var total = total1+10;

            //获取商品id号并构造列表
            $('.goods_list_td').each(function(){
                cart_ids.push($(this).attr('id'));
            });
            //将提交的订单保存到本地存储
{#            localStorage.setItem('order_finish',2);#}
            //向服务器发送post请求，传递参数：商品总价，id号列表，地址
            $.post({
                url:'/order/order_handle/',
                dataType:'json',
                type:'POST',
                data:{
                    'csrfmiddlewaretoken': '{{ csrf_token }}',
                    'cart_ids':cart_ids.join(','),
                    'total':total,
                    'address':address
                }
             })
            .done(function(data) {
                if (data.status == 1) {
                    $('.popup_con p').text('订单提交成功！');
                    $('.popup_con').fadeIn('fast', function () {
                        setTimeout(function () {
                            $('.popup_con').fadeOut('fast', function () {
                                window.location.href = '/user/order1/';
                            });
                        }, 3000)
                    });
                }
                else if (data.status == 0) {
                    $('.popup_con p').text('超出库存请修改！');
                    $('.popup_con').fadeIn('fast', function () {
                        setTimeout(function () {
                            $('.popup_con').fadeOut('fast', function () {
                                window.location.href = '/cart/';
                            });
                        }, 3000)
                    });
                }
                else if (data.status == 2) {
                    $('.popup_con p').text('网络异常，请稍后提交！');
                    $('.popup_con').fadeIn('fast', function () {
                        setTimeout(function () {
                            $('.popup_con').fadeOut('fast', function () {
                                window.location.href = '/cart/';
                            });
                        }, 3000)
                    });
                }
            });
        });
    });
</script>
{% endblock bottom %}>